package com.wayin.lift.controller.android;

import com.alibaba.fastjson.JSONObject;
import com.wayin.common.utils.PicUtils;
import com.wayin.common.utils.R;
import com.wayin.lift.entity.PersonnelEntity;
import com.wayin.lift.face.FaceCheck;
import com.wayin.lift.face.FaceHttp;
import com.wayin.lift.service.ClockInService;
import com.wayin.lift.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

import static com.wayin.lift.face.FaceCheck.faceFeatures;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Android接口
 *
 * @author HYXK
 * @email null@qq.com
 * @date 2020-05-26 09:56:38
 */
@Api(tags = "Android接口")
@RestController
@RequestMapping("/android")
public class AndroidController {

    @Autowired
    private PersonnelService personnelService;

    @Autowired
    private ClockInService clockInService;

    /**
     * 启动引擎
     */
    @ApiOperation("启动引擎")
    @RequestMapping(value = "/start", method = GET)
    public void start() throws Exception {
        String path1 = ClassUtils.getDefaultClassLoader().getResource("static/face").getPath();
        System.out.println(path1);
        List<PersonnelEntity> personnel = personnelService.findAllPersonnel();
        if (personnel.size() != faceFeatures.size()){
            FaceCheck.face4(personnel);
        }
    }

    /**
     * 打卡
     */
    @ApiOperation("打卡")
    @RequestMapping(value = "/clockIn", method = POST)
    public R clockIn(String openId, String temperature) throws ParseException {
//        com.alibaba.fastjson.JSONObject jsonParam = this.getJSONParam(request);
//        String openId = jsonParam.getString("openId");
//        String temperature = jsonParam.getString("temperature");
        if (openId == null) return R.error("打卡失败！");
        PersonnelEntity p = personnelService.findByOpenId(openId);
        int a = 0;
        switch (p.getStatus()){
            case "0":
                a = clockInService.clockIn(p.getPId(), temperature);
                break;
            case "1":
                break;
        }
        if (a == 0){
            return R.error("打卡失败！");
        } else if (a == 1){
            return R.error("请勿重复打卡！").put("floor", p.getFloor());
        }
        return R.ok("打卡成功！").put("floor", p.getFloor());
    }

    /**
     *
     *
     *
     *
     *
     *
     *
     * 下方为方法和测试接口
     */
    public JSONObject getJSONParam(HttpServletRequest request){
        JSONObject jsonParam = null;
        try {
            // 获取输入流
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

            // 写入数据到Stringbuilder
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
            // 直接将json信息打印出来
//            System.out.println(jsonParam.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }

    /**
     * 测试打卡
     */
    @ApiOperation("测试打卡")
    @RequestMapping(value = "/clockInTest", method = POST)
    public R clockInTest(String openId, String temperature) throws ParseException {
        if (openId == null) return R.error("打卡失败！");
        PersonnelEntity p = personnelService.findByOpenId(openId);
        int a = 0;
        switch (p.getStatus()){
            case "0":
                a = clockInService.clockIn(p.getPId(), temperature);
                break;
            case "1":
                break;
        }
        if (a == 0){
            return R.error("打卡失败！");
        } else if (a == 1){
            return R.error("请勿重复打卡！").put("floor", p.getFloor());
        }
        return R.ok("打卡成功！").put("floor", p.getFloor());
    }

}
